package array;

public class BinarySearDemo {
    public static int binarySearch(int[] array, int target) {
        int left = 0;
        int right = array.length - 1;
        while (left <= right) {
            //中间值
            int mid = left + (right - left) / 2;
            if (array[mid] == target) {
                return mid;
            } else if (array[mid] < target) {
                left++;

            } else {
                right++;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        int[] arr = {10,32,323,65,65,6,31,31,54,654};
        int search = binarySearch(arr, 31);
        System.out.printf(""+search);
    }

}
